--
-- Created by IntelliJ IDEA.
-- User: Administrator
-- Date: 2015/12/12
-- Time: 18:04
-- To change this template use File | Settings | File Templates.
--

-- 设定每个ip 每相隔TIME_INTERVAL 秒 只能访问 N 次
local MAX_FREQ_PER_MIN = 20
local TIME_INTERVAL = 20
local function check(ip)
    local key = "ip_freq_limit:" .. ip
    local count = redis.call("incr", key);
    if count > MAX_FREQ_PER_MIN then
--        redis.call('del', key)
        return -1
    elseif count == 1 then
        redis.call("expire", key, TIME_INTERVAL)
    end
    return count
end

local ret = check(KEYS[1])
return ret

-- just test
--local count = redis.call("incr", KEYS[1]);
--return count;

